<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--<!DOCTYPE html>--%>
<%--<html>--%>
<%--<head>--%>
<%--    <meta charset="utf-8">--%>
<%--    <title>layui</title>--%>
<%--    <meta name="renderer" content="webkit">--%>
<%--    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">--%>
<%--    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">--%>
<%--    <link rel="stylesheet" href="${bp}/layui/css/layui.css" media="all">--%>
<%--    <script src="${bp}/layui/layui.js" charset="utf-8"></script>--%>
<%--    <link rel="stylesheet" href="${bp}/layui/css/public.css" media="all">--%>
<%--</head>--%>
<%--<body>--%>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">所搜关键字</label>
                            <div class="layui-input-inline">
                                <input type="text" name="addId" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <%--        头部按钮--%>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete">批量删除</button>
            </div>
        </script>
        <%--行操作按钮--%>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>


    </div>
</div>

<script>

    var usr;
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: '${bp}/user/all',  // 更换为后台接口 返回json格式数据
            toolbar: '#toolbarDemo',   // 绑定头部工具栏
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 80},
                {field: 'addId',width: 100,title: '编号', sort: true},
                {field: 'addName',width: 150,title: '小区名称'},
                {field: 'addHouse',width: 150,title: '楼号', sort: true},
                {field: 'addUnit',width: 150,title: '单元号', sort: true},
                {field: 'addCount',width: 150,title: '住户数', sort: true},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100], // 下拉选的选项
            limit: 10,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            // js对象转化为json字符串
            var result = JSON.stringify(data.field);
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {  // 搜索条件
                    addId:data.field.addId
                }
            }, 'data');

            return false;
        })

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加地址信息',
                    type: 2,  // 引入外部的iframe
                    shade: 0.2,
                    shadeClose: true,
                    area: ['550px', '500px'],
                    content: 'page/d/add.jsp'
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;

                //    data  所有选中行的对象 [{},{}]    获取所有选中行的id  ->     => 箭头函数
                // function 函数名(){}     let fn = ()=>{} 匿名函数

                let ids = data.map(user => {
                    return user.addId
                }).join(",")

                // 发送ajax请求批量删除数据
                $.get("${bp}/user/delete",{ids},function (res) {
                    if (res.code == 0) {
                        layer.msg(res.msg, {icon: 1}, function () {
                            // 重载表格  刷新数据
                            table.reload('currentTableId', {});
                        })
                    } else { //  删除失败
                        layer.msg(res.msg, {icon:2})
                    }
                })
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            usr = data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '修改信息',
                    type: 2,
                    shade: 0.2,
                    shadeClose: true,
                    area: ['550px', '500px'],
                    content: 'page/d/edit.jsp'
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {

                    /*
                     * ES6
                     * let age = 10
                     * let name = 'lisi'
                     *
                     * let usr = {age,name}   {age:age,name:name}
                     */
                    // 发送异步请求  根据id 删除用户信息
                    let ids = data.addId


                    $.get("${bp}/user/delete", {ids}, function (res) {
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1}, function () {
                                // 重载表格  刷新数据
                                table.reload('currentTableId', {});
                            })
                        } else { //  删除失败
                            layer.msg(res.msg, {icon: 2})
                        }
                    }, "JSON")
                    layer.close(index);
                });
            }
        });

    });
</script>

</body>
</html>
